home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / faxrm / faxrm.c++ next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  193 lines

  1. /*    $Header: /usr/people/sam/fax/faxrm/RCS/faxrm.c++,v 1.18 1994/03/09 18:47:34 sam Rel $ */
  2. /*
  3.  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
  4.  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <stdarg.h>
  28.  
  29. #include "FaxClient.h"
  30. #include "StrArray.h"
  31. #include "config.h"
  32.  
  33. class faxRmApp : public FaxClient {
  34. private:
  35.     fxStr    appName;        // for error messages
  36.     fxStrArray    jobids;
  37.     fxStr    request;
  38.  
  39.     void usage();
  40.     void printError(const char* fmt, ...);
  41.     void printWarning(const char* fmt, ...);
  42. public:
  43.     faxRmApp();
  44.     ~faxRmApp();
  45.  
  46.     void initialize(int argc, char** argv);
  47.     void open();
  48.  
  49.     void recvConf(const char* cmd, const char* tag);
  50.     void recvEof();
  51.     void recvError(const int err);
  52. };
  53.  
  54. faxRmApp::faxRmApp() : request("remove") {}
  55. faxRmApp::~faxRmApp() {}
  56.  
  57. void
  58. faxRmApp::initialize(int argc, char** argv)
  59. {
  60.     extern int optind;
  61.     extern char* optarg;
  62.     int c;
  63.  
  64.     appName = argv[0];
  65.     u_int l = appName.length();
  66.     appName = appName.tokenR(l, '/');
  67.     while ((c = getopt(argc, argv, "h:kv")) != -1)
  68.     switch (c) {
  69.     case 'h':            // server's host
  70.         setHost(optarg);
  71.         break;
  72.     case 'k':
  73.         request = "kill";
  74.         break;
  75.     case 'v':
  76.         setVerbose(TRUE);
  77.         break;
  78.     case '?':
  79.         usage();
  80.     }
  81.     if (optind >= argc)
  82.     usage();
  83.     setupUserIdentity();
  84.     for (; optind < argc; optind++)
  85.     jobids.append(argv[optind]);
  86. }
  87.  
  88. void
  89. faxRmApp::usage()
  90. {
  91.     fxFatal("usage: %s [-h server-host] [-kv] jobID...", (char*) appName);
  92. }
  93.  
  94. void
  95. faxRmApp::open()
  96. {
  97.     if (callServer()) {
  98.     for (int i = 0; i < jobids.length(); i++) {
  99.         fxStr line = jobids[i] | ":" | getSenderName();
  100.         sendLine(request, line);
  101.     }
  102.     sendLine(".\n");
  103.     startRunning();
  104.     } else
  105.     fxFatal("Could not call server.");
  106. }
  107.  
  108. void
  109. faxRmApp::printError(const char* va_alist ...)
  110. #define    fmt va_alist
  111. {
  112.     va_list ap;
  113.     va_start(ap, va_alist);
  114.     fprintf(stderr, "%s: ", (char*) appName);
  115.     vfprintf(stderr, fmt, ap);
  116.     va_end(ap);
  117.     fputs("\n", stderr);
  118.     exit(-1);
  119. }
  120. #undef fmt
  121.  
  122. void
  123. faxRmApp::printWarning(const char* va_alist ...)
  124. #define    fmt va_alist
  125. {
  126.     va_list ap;
  127.     va_start(ap, va_alist);
  128.     fprintf(stderr, "%s: Warning, ", (char*) appName);
  129.     vfprintf(stderr, fmt, ap);
  130.     va_end(ap);
  131.     fputs("\n", stderr);
  132. }
  133. #undef fmt
  134.  
  135. #define isCmd(s)    (strcasecmp(cmd, s) == 0)
  136.  
  137. void
  138. faxRmApp::recvConf(const char* cmd, const char* tag)
  139. {
  140.     if (isCmd("removed")) {
  141.     printf("Job %s removed.\n", tag);
  142.     } else if (isCmd("notQueued")) {
  143.     printf("Job %s not removed: it was not queued.\n", tag);
  144.     } else if (isCmd("jobLocked")) {
  145.     printf("Job %s not removed: it is being sent.\n", tag);
  146.     } else if (isCmd("openFailed")) {
  147.     printf("Job %s not removed: open failed; check SYSLOG.\n", tag);
  148.     } else if (isCmd("unlinkFailed")) {
  149.     printf("Job %s not removed: unlink failed; check SYSLOG.\n", tag);
  150.     } else if (isCmd("docUnlinkFailed")) {
  151.     printf(
  152.         "Document for job %s not removed: unlink failed; check SYSLOG.\n",
  153.         tag);
  154.     } else if (isCmd("sOwner")) {
  155.     printf(
  156.         "Job %s not removed: could not establish server process owner.\n",
  157.         tag);
  158.     } else if (isCmd("jobOwner")) {
  159.     printf("Job %s not removed: you neither own it nor are the fax user.\n",
  160.         tag);
  161.     } else if (isCmd("error")) {
  162.     printf("%s\n", tag);
  163.     } else {
  164.     printf("Unknown status message \"%s:%s\"\n", cmd, tag);
  165.     }
  166. }
  167.  
  168. void
  169. faxRmApp::recvEof()
  170. {
  171.     stopRunning();
  172. }
  173.  
  174. void
  175. faxRmApp::recvError(const int err)
  176. {
  177.     printf("Fatal socket read error: %s\n", strerror(err));
  178.     stopRunning();
  179. }
  180.  
  181. #include <Dispatch/dispatcher.h>
  182.  
  183. extern "C" int
  184. main(int argc, char** argv)
  185. {
  186.     faxRmApp app;
  187.     app.initialize(argc, argv);
  188.     app.open();
  189.     while (app.isRunning())
  190.     Dispatcher::instance().dispatch();
  191.     return 0;
  192. }
  193.